#include "mysql_info.h"
const char host[]="127.0.0.1";
const char user[]="root";
const char password[]="cody0s";
const char db_name[]="cloud_storage";
//获取一个mysql连接
MYSQL* get_one_mysql_connect(){
    MYSQL *mysql=mysql_init(NULL);                                                                                                              
    if(!mysql){
        return NULL;
        fprintf(stderr,"%s","mysql_init");
    }
    void * real_connect_ret=mysql_real_connect(mysql,host,user,password,db_name,3306,NULL,0);
    printf("real_connect_ret = %d\n",real_connect_ret);
    if(!real_connect_ret){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return NULL;
    }
    return mysql; 
}

int get_user_by_id(MYSQL* mysql,user_info_t *user,int user_id){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select * from user_info where id=%d and is_deleted=0",user_id);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    MYSQL_ROW row;
    while((row=mysql_fetch_row(res))!=NULL){
        user->id=atoi(row[0]);
        strcpy(user->user_name,row[1]);
        strcpy(user->salt,row[2]);
        strcpy(user->encrypted_password,row[3]);
        user->is_deleted=atoi(row[4]);
    }
    mysql_free_result(res); // 释放结果集
    return 1;
}

int is_user_exist(MYSQL*mysql,int user_id){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select id from user_info where id=%d and is_deleted=0",user_id);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(res);
    if(row==NULL){
        return 0;
    }
    mysql_free_result(res); // 释放结果集
    return 1;
}

int add_one_user(MYSQL*mysql,user_info_t *user){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),
             "insert into user_info (user_name, salt, encrypted_password, is_deleted) values ('%s','%s','%s',%d)",
             user->user_name,user->salt,user->encrypted_password,0);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    user->id=mysql_insert_id(mysql);
    return 1;
}
int valid_user(MYSQL*mysql,user_info_t *user){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),
             "select user_name,encrypted_password  from user_info where user_name='%s'",user->user_name);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -2;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    mysql_store_result(mysql);
    MYSQL_ROW row;
    row=mysql_fetch_row(res);
    if(row==NULL){
        return -2;
    }
    if(strcmp(user->user_name,row[0])==0&&strcmp(user->encrypted_password,row[1])==0){
        mysql_free_result(res); // 释放结果集
        return 1;
    }
    mysql_free_result(res); // 释放结果集
    return -1;
}
int is_exist_username(MYSQL*mysql,char *user_name){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select id from user_info where user_name='%s' and is_deleted=0",user_name);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        printf("进入mysqlquery\n");
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    printf("没进mysqlquery\n");
    MYSQL_RES *res=mysql_store_result(mysql);
    mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(res);
    if(row==NULL){
        return 0;
    }
    mysql_free_result(res); // 释放结果集
    return 1;
}
int get_user_id_by_user_name(MYSQL*mysql,char * user_name){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select id from user_info where user_name='%s' and is_deleted=0",user_name);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(res);
    if(row==NULL){
        return -1;
    }
    int ret=atoi(row[0]);
    mysql_free_result(res); // 释放结果集
    return ret;
}

int get_user_name_by_id(MYSQL*mysql,char* user_name,int id){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select user_name from user_info where id=%d and is_deleted=0",id);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(res);
    if(row==NULL){
        return -1;
    }
    strcpy(user_name,row[0]);
    mysql_free_result(res); // 释放结果集
    return 1;
}
int log_info(MYSQL* mysql, int user_id,const char *op, int status, const char *msg) {
    /* // 获取当前时间 */
    /* char query[3000]={0}; */
    /* time_t now = time(NULL); */
    /* struct tm *tm_info = localtime(&now); */

    /* // 格式化为YYYY-MM-DD HH:MM:SS */
    /* char datetime_str[20]; */
    /* strftime(datetime_str, sizeof(datetime_str), "%Y-%m-%d %H:%M:%S", tm_info); */
    /* char escaped_op[1024]; */
    /* char escaped_msg[1024]; */
    /* mysql_real_escape_string(mysql, escaped_op, op, strlen(op)); */
    /* mysql_real_escape_string(mysql, escaped_msg, msg, strlen(msg)); */

    /* bzero(query,sizeof(query)); */
    /* snprintf(query, sizeof(query), */ 
    /*     "INSERT INTO user_log (user_id, op_time, op, status, msg) " */
    /*     "VALUES (%d, '%s', '%s', %d, '%s')", */ 
    /*     user_id, datetime_str, escaped_op, status, escaped_msg); */

    /* if (mysql_query(mysql, query)) { */
    /* fprintf(stderr, "SQL执行失败: %s\n", mysql_error(mysql)); */
    /* return -1; */
    /* } */

    /* return 1; */
    // 函数内部实现不变（因为mysql_real_escape_string的第三个参数就是const char*）
    char query[3000] = {0};
    time_t now = time(NULL);
    struct tm *tm_info = localtime(&now);
    char datetime_str[20];
    strftime(datetime_str, sizeof(datetime_str), "%Y-%m-%d %H:%M:%S", tm_info);

    char escaped_op[1024];
    char escaped_msg[1024];
    mysql_real_escape_string(mysql, escaped_op, op, strlen(op)); 
    mysql_real_escape_string(mysql, escaped_msg, msg, strlen(msg));

    snprintf(query, sizeof(query),
             "INSERT INTO user_log (user_id, op_time, op, status, msg) "
             "VALUES (%d, '%s', '%s', %d, '%s')",
             user_id, datetime_str, escaped_op, status, escaped_msg);

    if (mysql_query(mysql, query)) {
        fprintf(stderr, "SQL执行失败: %s\n", mysql_error(mysql));
        return -1;
    }
    return 1;
}

int get_last_login_time(MYSQL*mysql,char *last_login_time,int user_id){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select op_time  from user_log  where user_id=%d and status=%d order by op_time desc limit 1",
             user_id,1);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    /* mysql_store_result(mysql); */
    MYSQL_ROW row=mysql_fetch_row(res);
    if(row==NULL){
        return -1;
    }
    strcpy(last_login_time,row[0]);
    mysql_free_result(res); // 释放结果集
    return 1;
}
/* igccnt recent_use_cmd(MYSQL*mysql,char *recent_use_cmd,int user_id){ */
/* char query[3000]={0}; */
/* bzero(query,sizeof(query)); */
/* snprintf(query,sizeof(query),"select op from user_log  where user_id=%d and status=%d", */
/* user_id,1); */
/* printf("%s\n",query); */
/* if(mysql_query(mysql,query)){ */
/*     fprintf(stderr,"%s\n",mysql_error(mysql)); */
/*     return -1; */
/* } */
/* MYSQL_RES *res=mysql_store_result(mysql); */
/* /1* mysql_store_result(mysql); *1/ */
/* MYSQL_ROW row=mysql_fetch_row(res); */
/* if(row==NULL){ */
/*     return -1; */
/* } */
/* strcpy(recent_use_cmd,row[0]); */
/* int i=1; */
/* while((row=mysql_fetch_row(res))!=NULL){ */
/*     strcat(recent_use_cmd,row[i]); */
/*     i++; */
/* } */
/* return 1; */
int recent_use_cmd(MYSQL* mysql, char *recent_use_cmd, int user_id) {
    char query[3000] = {0};
    snprintf(query, sizeof(query),
             "SELECT op,msg FROM user_log WHERE user_id=%d AND status=%d AND op <> 'login' AND op<> 'register' ORDER BY op_time DESC LIMIT 10",
             user_id, 1);
    printf("%s\n", query);

    if (mysql_query(mysql, query)) {
        fprintf(stderr, "%s\n", mysql_error(mysql));
        return -1;
    }

    MYSQL_RES *res = mysql_store_result(mysql);
    if (res == NULL) {
        fprintf(stderr, "mysql_store_result() failed\n");
        return -1;
    }

    int result = 0;

    MYSQL_ROW row;
    char str[10]={0};
    while((row=mysql_fetch_row(res))!=NULL){
        result++;
        bzero(str,sizeof(str));
        snprintf(str,sizeof(str),"%d",result);
        strcat(recent_use_cmd,str);
        strcat(recent_use_cmd," ");
        strcat(recent_use_cmd,row[0]);
        strcat(recent_use_cmd," ");
        strcat(recent_use_cmd,row[1]);
        strcat(recent_use_cmd,"\n");
    }
    recent_use_cmd[strlen(recent_use_cmd)-1]='\0';
    recent_use_cmd[strlen(recent_use_cmd)]='\0';
    mysql_free_result(res); // 释放结果集
    return result;
}

void close_mysql_connect(MYSQL*mysql){
    mysql_close(mysql);
}
void print_user(user_info_t *user){
    printf("\n user_id:%d user_name%s,salt:%s,encrypted_password:%s,is_deleted:%d\n",
           user->id,user->user_name,user->salt,user->encrypted_password,user->is_deleted);    
}

int get_user_name_by_vid(MYSQL*mysql,int id,char *user_name){
    char query[3000]={0};
    bzero(query,sizeof(query));
    snprintf(query,sizeof(query),"select user_name from virtual_directory  where id=%d and is_deleted=0",id);
    printf("%s\n",query);
    if(mysql_query(mysql,query)){
        fprintf(stderr,"%s\n",mysql_error(mysql));
        return -1;
    }
    MYSQL_RES *res=mysql_store_result(mysql);
    MYSQL_ROW row=mysql_fetch_row(res);
    if(row==NULL){
        return -1;
    }
    strcpy(user_name,row[0]);
    mysql_free_result(res); // 释放结果集
    return 0;
}
